/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.impl;

import java.util.ArrayList;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author rafael
 */
public final class NotificarEmail {

    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final String SMTP_AUTH_USER = "karangooscar@gmail.com";
    private static final String SMTP_AUTH_PWD = "KarangoosCar123";
    private static final String saudacao = "Caro ";
    private static final String corpoEmail = ", temos um comprador para o seu carro! ";

    public void sendEmail(String veiculo, String comprador, String vendedor, String emailVendedor) {


        try {
            Properties props = new Properties();
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.host", SMTP_HOST_NAME);
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.starttls.enable", "true");

            Authenticator auth = new SMTPAuthenticator();

            Session mailSession = Session.getInstance(props, auth);
            mailSession.setDebug(true);

            Transport transport = mailSession.getTransport();

            MimeMessage message = new MimeMessage(mailSession);

            message.setSubject("Karangoos Car");
            message.setContent(getCorpoEmail(vendedor, comprador, veiculo), "text/plain");
            message.setFrom(new InternetAddress(SMTP_AUTH_USER));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailVendedor));

            transport.connect();
            transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
            transport.close();
            GrowMessages.alertMessage("Nova compra efetuada", "Um email foi enviado para o vendedor!");
        } catch (Exception e) {
            System.out.println(e.getLocalizedMessage());
        }
    }

    private String getCorpoEmail(String vendedor, String comprador, String veiculo) {
        ArrayList<String> conteudo = new ArrayList<String>();
        conteudo.add(saudacao);
        conteudo.add(vendedor);
        conteudo.add(corpoEmail);
        conteudo.add("O usuario ");
        conteudo.add(comprador);
        conteudo.add(" se interessou no seu ");
        conteudo.add(veiculo);
        conteudo.add("!");
        String content = "";
        for (int x = 0; x < conteudo.size(); x++) {
            content = content.concat(conteudo.get(x));
        }
        return content;
    }

    private class SMTPAuthenticator extends javax.mail.Authenticator {

        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            String username = SMTP_AUTH_USER;
            String password = SMTP_AUTH_PWD;
            return new PasswordAuthentication(username, password);
        }
    }
}
